<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>else</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="control-structures.if.html">« if</a></li>
      <li style="float: right;"><a href="control-structures.elseif.html">elseif/else if »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.control-structures.html">流程控制</a></li>
    <li>else</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="control-structures.else" class="sect1">
 <h2 class="title">else</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="para">
  经常需要在满足某个条件时执行一条语句，而在不满足该条件时执行其它语句，这正是
  <code class="literal">else</code> 的功能。<code class="literal">else</code> 延伸了
  <code class="literal">if</code> 语句，可以在 <code class="literal">if</code>
  语句中的表达式的值为 <strong><code>false</code></strong> 时执行语句。例如以下代码在
  <var class="varname">$a</var> 大于 <var class="varname">$b</var> 时显示
  <span class="computeroutput">a is bigger than b</span>，反之则显示
  <span class="computeroutput">a is NOT bigger than b</span>：
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;greater&nbsp;than&nbsp;b"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;NOT&nbsp;greater&nbsp;than&nbsp;b"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <code class="literal">else</code> 语句仅在 <code class="literal">if</code> 以及
  <code class="literal">elseif</code>（如果有的话）语句中的表达式的值为
  <strong><code>false</code></strong> 时执行（参见 <a href="control-structures.elseif.html" class="link">elseif</a>）。
 </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
  <strong>悬挂的 else</strong><br />
  <p class="para">
   在多层嵌套 <code class="literal">if</code>-<code class="literal">else</code> 语句的情况下，
   <code class="literal">else</code> 总是与最近的 <code class="literal">if</code> 进行关联。
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$b</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">;<br />else<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   虽然存在缩进（对 PHP 来说，无关紧要）， 但是 <code class="literal">else</code> 还是与 <code class="literal">if ($b)</code> 
   进行关联，所以以上示例不会产生任何输出。虽然可以依赖此特性，但是推荐使用花括号，避免潜在的歧义问题。
  </p>
 </p></blockquote>
</div></div></div></body></html>